package com.hdu;

import java.util.concurrent.*;

import static java.util.concurrent.TimeUnit.SECONDS;


public class Main {
    public static void main(String[] args) {
        testHandleException();
    }


    public static void testHandleException() {
        ExecutorService threadPool = new ThreadPoolExecutor(
                Runtime.getRuntime().availableProcessors(),
                Runtime.getRuntime().availableProcessors() * 2,
                1,
                SECONDS,
                new LinkedBlockingQueue<>(100)
        ) {
            @Override
            protected void afterExecute(Runnable r, Throwable t) {
                if (t != null) {
                    System.out.println(t.getMessage());
                }
                if (t == null && r instanceof Future) {
                    try {
                        if (((Future<?>) r).isDone()) {
                            ((Future<?>) r).get();
                        }
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                }
            }
        };

        threadPool.submit(() -> {
            throw new RuntimeException("submitException");
        });

        Future<Object> future = threadPool.submit(() -> {
            throw new RuntimeException("submitAndGetException");
        });

        try {
            future.get();
        } catch (Exception e) {
            System.out.println("future.get() 出现了异常");
        }

        threadPool.execute(() -> {
            throw new RuntimeException("execute Exception");
        });

        // java.lang.RuntimeException: submitException
        // java.lang.RuntimeException: submitAndGetException
        // future.get() 出现了异常
        // execute Exception
        // Exception in thread "pool-1-thread-3" java.lang.RuntimeException: execute Exception
        //	at com.hdu.Main.lambda$testHandleException$2(Main.java:54)
        //	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        //	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        //	at java.lang.Thread.run(Thread.java:748)
    }


    public static void testCommon() {
        ExecutorService threadPool = Executors.newFixedThreadPool(1);

        threadPool.submit(() -> {
            throw new RuntimeException("test");
        });

        Future<Object> future = threadPool.submit(() -> {
            throw new RuntimeException("test");
        });

        try {
            future.get();
        } catch (Exception e) {
            System.out.println("future.get() 出现了异常");
        }

        threadPool.execute(() -> {
            throw new RuntimeException("test");
        });

        //  future.get() 出现了异常
        //  Exception in thread "pool-1-thread-1" java.lang.RuntimeException: test
        //	at com.hdu.Main.lambda$main$2(Main.java:27)
        //	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        //	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        //	at java.lang.Thread.run(Thread.java:748)
    }
}